﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Templates_NewsSection_Default : BaseTemplatePage
{
    protected override void AttachEvents()
    {
        ucFeedModule.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(ucFeedModule_SelectData);
        ucExploreModule.DataRequired += new EventHandler(ucExploreModule_DataRequired);
    }

    protected void btnApplyFilter_Click(object sender, EventArgs e)
    {
        List<int> filters = new List<int>();
        List<Topic> services = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.CurrentPost.SiteId, "Services");
        for(int i = 0; i < cblServices.Items.Length; i++)
        {
            if (cblServices.Items[i].Checked)
            {
                filters.Add(services[i].Id);
            }
        }
        /*List<Topic> sectors = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.CurrentPost.SiteId, "Sectors");
        for (int i = 0; i < cblSectors.Items.Length; i++)
        {
            if (cblSectors.Items[i].Checked)
            {
                filters.Add(sectors[i].Id);
            }
        }*/
        List<Topic> people = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.CurrentPost.SiteId, "People");
        for (int i = 0; i < cblPeople.Items.Length; i++)
        {
            if (cblPeople.Items[i].Checked)
            {
                filters.Add(people[i].Id);
            }
        }
        FilterIds = filters.ToArray();
        ucFeedModule.Rebind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (AcmsContext.CurrentPost != null)
            {
                string[] nodes = AcmsContext.CurrentPost.Path.Split('/');
                if (nodes.Length == 2)
                {
                    ucFeedModule.Title = string.Concat("Latest ", AcmsContext.CurrentPost.DisplayName);
                }
                else
                {
                    ucFeedModule.Title = AcmsContext.CurrentPost.DisplayName;
                }
            }
            else
            {
                ucFeedModule.Title = "Latest Display Name Here";
            }
            DataBind();
        }
    }

    void ucExploreModule_DataRequired(object sender, EventArgs e)
    {
        ucExploreModule.DataSource = GetExploreData();
    }

    private List<Post> listData;
    protected List<Post> GetListData()
    {
        if (listData == null)
        {
            OrgasiniseData();
        }
        return listData;
    }

    private List<Post> exploreData;
    protected List<Post> GetExploreData()
    {
        if (exploreData == null)
        {
            OrgasiniseData();
        }
        return exploreData;
    }

    int[] FilterIds;

    private void OrgasiniseData()
    {
        if (AcmsContext.CurrentPost != null)
        {
            listData = new List<Post>();
            exploreData = new List<Post>();
            List<Post> children = null;
            if (FilterIds == null || FilterIds.Length == 0)
            {
                children = AcmsContext.CurrentPost.Children;
            }
            else if (FilterIds.Length == 1)
            {
                children = Post.GetAllByTopicIdAndParentPath(AcmsContext.SiteId, FilterIds[0], AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
            }
            else
            {
                children = Post.GetAllByTopicIdsAndParentPath(AcmsContext.SiteId, FilterIds, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
            }
            foreach (Post p in children)
            {
                if (p.TemplateId == AcmsContext.CurrentPost.TemplateId)
                {
                    exploreData.Add(p);
                }
                else
                {
                    listData.Add(p);
                }
            }
        }
        else
        {
            exploreData = DummyPost.CreateDummyList(5);
            listData = DummyPost.CreateDummyList(12);
        }
    }

    void ucFeedModule_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        e.SetData(GetListData());
    }

    protected override void DataBindChildren()
    {
        cblPeople.DataSource = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.SiteId, "People");
        cblServices.DataSource = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.SiteId, "Services");
        //cblSectors.DataSource = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.SiteId, "Sectors");
        accordianFilterPane.DataBind();
        base.DataBindChildren();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        FilterIds = ViewState["FilterIDs"] as int[];
    }

    protected override object SaveViewState()
    {
        ViewState["FilterIDs"] = FilterIds;
        return base.SaveViewState();
    }
}
